﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.Hosting;


namespace AspNetThemesExtensions
{
    /// <summary>
    /// Wraps logic for directories
    /// </summary>
    public class ThemeVirtualDirectory : VirtualDirectory
    {
        private string _virtualPath;
        private VirtualDirectory _wrappedDir;

        private List<VirtualFileBase> _children;
        private List<VirtualFileBase> _files;
        private List<VirtualFileBase> _dirs;

        public ThemeVirtualDirectory(string virtualPath,VirtualDirectory wrappedDir) : base(virtualPath)
        {
            _virtualPath = virtualPath;
            _wrappedDir = wrappedDir;

        }
        
        public override System.Collections.IEnumerable Children
        {
            get
            { 
                if(_children==null)
                    _children = Wrap(true, true);

                return _children;
            }
        }

        public override System.Collections.IEnumerable Directories
        {
            get
            {
                if (_dirs == null)
                    _dirs = Wrap(false, true);

                return _dirs;
            }
        }

        public override System.Collections.IEnumerable Files
        {
            get
            {
                if (_files == null)
                    _files = Wrap(true,false);

                return _files;
            }
        }

        private List<VirtualFileBase> Wrap(bool handleFiles, bool handleDirs)
        {
            List<VirtualFileBase> l = new List<VirtualFileBase>();
            

            if (handleDirs)
            {
                WrapDirs(_wrappedDir!=null ? _wrappedDir.Directories : null, l);
            }

            if (handleFiles)
            {
                WrapFiles(_wrappedDir != null ? _wrappedDir.Files : null, l);
            }

            return l;
        }

        private void WrapFiles(IEnumerable wrap, List<VirtualFileBase> l)
        {
            Hashtable lookup = new Hashtable(StringComparer.OrdinalIgnoreCase);

            if (wrap != null)
            {
                foreach (VirtualFileBase c in wrap)
                {
                    if (c is VirtualFile)
                    {
                        l.Add(new ThemeVirtualFile(_virtualPath + "/" + c.Name, c.VirtualPath));
                        lookup.Add(c.Name, c);
                    }
                }
            }

            DirectoryInfo d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~" + _virtualPath));
            if (d.Exists)
            {
                foreach (FileInfo f in d.GetFiles())
                {
                    if (!lookup.Contains(f.Name))
                        l.Add(new ThemeVirtualFile(_virtualPath + "/" + f.Name, _wrappedDir.VirtualPath + "/" + f.Name));
                }
            }
        }

        private void WrapDirs(IEnumerable wrap, List<VirtualFileBase> l)
        {
            Hashtable lookup = new Hashtable(StringComparer.OrdinalIgnoreCase);

            if (wrap != null)
            {
                foreach (VirtualFileBase c in wrap)
                {
                    if (c is VirtualDirectory)
                    {
                        l.Add(new ThemeVirtualDirectory(_virtualPath + "/" + c.Name, (VirtualDirectory)c));
                        lookup.Add(c.Name, c);
                    }
                }
            }


            DirectoryInfo d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~" + _virtualPath));
            if (d.Exists)
            {
                foreach (DirectoryInfo sub in d.GetDirectories())
                {
                    if (!lookup.Contains(sub.Name))
                        l.Add(new ThemeVirtualDirectory(_virtualPath + "/" + sub.Name, null));
                }
            }
        }



    }
}
